<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 /**
 * =================================================================
 * Tokaban Standard System.
 * CodeIniter Tokaban framework for PHP.
 *
 * @package		: CI-TKB 
 * @author		: Tokaban R&D Team.
 * 				: an_hm87
 * @copyright	: Copyright (c) 2009, Tokaban, Inc.
 * @since		: Version 2.0
 * =================================================================
 */    
   
/**
 *------------------------------------------------------------------
 * do_product class
 *
 * Quan ly them, sua thong tin danh muc san pham
 *
 * @subpackage	controllers
 * @category	
 * @author		Hoang Minh An 
 *------------------------------------------------------------------
 */     
class do_product extends Controller  
{
//He thong	
	var $m_language 		= ''; // nhan ngon ngu tu file language
	var $m_nid_user_login 	= ''; // nhan iduser tu session

	var $m_nid 				= ''; // nhan nid cua phuong xa can chinh sua
	var $m_event			= ''; // nhan su kien cap nhat hoac bo qua
	var $m_button_click		= ''; // nhan su kien tu hidden button o trang ap_product
	
	var	$m_link_page  		= ''; // nhan link cua su kien
	var $m_link_cancel 		='';  // link toi trang ap_product_listview

	var $m_form_title 		= ''; // tieu de cua form

// Nhung bien dung cho doi tuong.			  
	var $m_txt_nid 			  		= ''; // nhan ma tin 
	var $m_txt_ccode			  	= '';// dai dien cho bien ccode
	var $m_txt_cproducts 			= '';
	var $m_txt_cnote	 			= '';
	var $m_chk_nspecial_product		= 0;
	var $m_txt_nstatus				= '';
	var $m_txt_cindex				= '';
	var $m_txt_user01				= '';
	var $m_txt_ddate01				= '';
	var $m_txt_ctag					= '';	
	//var $m_txt_cmodel				= '';
	var $m_txt_cpower				= '';
	var $m_txt_cimage				= '';
	var $m_hidden_image_old			= '';
	var $m_txt_cimage_resize		= '';
	var $m_hidden_image_old_resize	= '';
	
	var $m_txt_fprice				= '';
	var $m_txt_fprice_sale			= '';
	//var $m_txt_nquantity			= '';
	var $m_txt_cdescription			= '';
	var $m_txt_cdetail				= '';
	var $nid_brand_products			= '';
	
	var $m_cbo_nid_material_products = '';
	var $m_cbo_nid_cat_products 	= '';
	var $m_cbo_nid_weight		 	= '';

	var $m_obj_material_product_view = '';
	var $m_obj_cat_product_view 	= '';
	var $m_obj_brand			 	= '';
	var $m_obj_weight_view 			= '';
	
	var $m_obj_data_view    		= '';
	
	// Cac bien can xuat hien thi thong bao len view cho nguoi dung xem.
	var $m_error_msg 	    		= ''; 
	var $m_link_cancel_trans	= '';
	
/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com 
 * @finished date	: 2009/12/13
 * @description		: Ham khoi tao, load cac thu vien can dung cho class
 * @access	        : public
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */	  
function do_product()
	{ 			
		
		parent::Controller();
		session_start();
		//Load cac thu vien he thong.
		$this->load->database();
		
		$this->load->helper('ap_db');	
		$this->load->helper('ap_function');
		$this->load->helper('ap_html');
		$this->load->helper('ap_view');
		$this->load->helper('ap_object');
		$this->load->helper('ap_fck'); // load thu vien de xuat ra trinh soan thao van ban
				
		// Kiem tra dieu kien login theo ma so he thong 1
		$this->tokaban_system_check = '1';
		// Load cac thu vien rieng can thiet khac neu co
		$this->load->model('product_model');
		
		init_fck();
		$this->load->library('fckeditor');
		$this->fckeditor->BasePath = base_url().'ap_application/plugins/fckeditor/';
		
	}
	
	/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Lay nid tu view ap_product_listview
 * @access	        : public
 *
 * @param string	: $nid   : truong khoa chinh cua tproduct
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
 
 private function m_language_key($str_key)
	{
		return $this->lang->line('lbl.product.'.$str_key);
	}
	
/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Lay nid tu view ap_product_listview
 * @access	        : public
 *
 * @param string	: $nid   : truong khoa chinh cua tproduct
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function f_edit($nid) // ham chinh sua, tham so la nid cua phuong xa can chinh sua
	{
		$this->m_event = 'edit';
		$this->m_nid   = $nid;		 	
		$this->do_process();		
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: 
 * @access	        : public
 *
 * @param string	: None
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function f_update_edit() // ham update cac thong tin da chinh sua
	{	
		$this->m_event = 'update_edit';		
		$this->do_process();
	}





/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: 
 * @access	        : public
 *
 * @param string	: None
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function f_add() // ham them phuong xa
	{				
		$this->m_event 	= 'add';
		$this->m_nid 	= '0';
		$this->do_process();
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: 
 * @access	        : public
 *
 * @param string	: None
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function f_update_add() // ham cap nhat thong tin cua phuong xa moi can tao
	{		
		$this->m_event = 'update_add';
		$this->do_process();
				
	}
	
	
	
/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/11/11
 * @description		: Goi tuan tu cac ham theo dung quy dinh ve luong du lieu
 * @access	        : public
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
function do_process() // ham xu ly
	{
		$this->get_data();
		$this->caculate_data();
		$this->do_business();
		$this->destroy_data();
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Nhan du lieu
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */
private function get_data()
	{
// Lay nid user tu session
		$this->m_nid_user_login = Fget_userdata('session_nid_user');
// Load file ngon ngu can su dung
		$this->load->language('ap', 'eng');
		
		// Kiem tra hinh anh duoc upload
		if(isset($_FILES["txt_cimage"]))
		{
			if($_FILES["txt_cimage"]["name"]!='')
			{
				$path_full					= '.././upload/images_product/full_images/';
				$this->m_txt_cimage 		= Fupload_resize_img($_FILES["txt_cimage"],$path_full,600,574);
				
				$path_detail				= '.././upload/images_product/resize_images/';
				$this->m_txt_cimage_resize	= resize_img($_FILES["txt_cimage"],$this->m_txt_cimage,$path_full.$this->m_txt_cimage,$path_detail,209,200);
				
				if($this->m_txt_cimage != '')
				{
					$path				='.././upload/images_product/full_images/';
					delfile($path.$_POST['hidden_image_old']);
					$this->m_hidden_image_old	= $this->m_txt_cimage;		
				}
				else
					$this->m_hidden_image_old	= $_POST['hidden_image_old'];
				if($this->m_txt_cimage_resize != '')
				{
					$path				='.././upload/images_product/resize_images/';
					delfile($path.$_POST['hidden_image_old_resize']);
					$this->m_hidden_image_old_resize	= $this->m_txt_cimage_resize;		
				}
				else
					$this->m_hidden_image_old_resize	= $_POST['hidden_image_old'];
			}									
		}
		
		if(!empty($_POST['chk_nspecial_product']))
			$this->m_chk_nspecial_product	= $_POST['chk_nspecial_product'];
		
		if (isset($_POST['txt_ccode']))
		{	
			$this->m_txt_ccode		 		= trim($_POST['txt_ccode']);
			$this->m_txt_cproducts		 	= trim($_POST['txt_cproducts']);
			$this->m_txt_cnote			 	= trim($_POST['txt_cnote']);
			$this->nid_brand_products		= trim($_POST['nid_brand_products']);			
			$this->m_txt_cindex		 		= $_POST['txt_cindex'];
			$this->m_txt_nstatus		 	= $_POST['txt_nstatus'];
			$this->m_txt_ctag				= $_POST['txt_ctag'];
			//$this->m_txt_cmodel				= $_POST['txt_cmodel'];
			$this->m_txt_cpower				= $_POST['txt_cpower'];
			$this->m_txt_fprice			 	= $_POST['txt_fprice'];
			$this->m_txt_fprice_sale	 	= $_POST['txt_fprice_sale'];
			//$this->m_txt_nquantity		 	= $_POST['txt_nquantity'];
			$this->m_txt_cdescription	 	= $_POST['txt_cdescription'];
			$this->m_txt_cdetail		 	= $_POST['txt_cdetail'];
		}
		if(isset($_POST['cbo_nid_material_products']))	
			$this->m_cbo_nid_material_products =   $_POST['cbo_nid_material_products'];
		if(isset($_POST['cbo_nid_cat_products']))	
			$this->m_cbo_nid_cat_products 		=   $_POST['cbo_nid_cat_products'];
		if(isset($_POST['cbo_nid_weight']))	
			$this->m_cbo_nid_weight 		=   $_POST['cbo_nid_weight'];
			
// Kiem tra va nhan cac bien hidden neu co.
		if (isset($_POST['hidden_nid']))
			$this->m_nid 			= $_POST['hidden_nid'];
			
		if (isset($_POST['hidden_event']))
			$this->m_event			= $_POST['hidden_event'];
			
		if (isset($_POST['hidden_button_click']))
			$this->m_button_click	= $_POST['hidden_button_click'];
		
		if (isset($_POST['hidden_image_old']))
			$this->m_hidden_image_old			= $_POST['hidden_image_old'];
		if (isset($_POST['hidden_image_old_resize']))
			$this->m_hidden_image_old_resize	= $_POST['hidden_image_old_resize'];
	
			
	}
		

/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Tinh toan du lieu
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function caculate_data()
	{
// Link khi click nut cancel
		$this->m_link_page 	= base_url() . 'index.php/do_product/f_update_edit';	
		$this->m_link_cancel = base_url() . 'index.php/do_product_listview';	
		
		switch ($this->m_event)
		{
			case 'edit':	
				$obj_data = $this->product_model->get_byid($this->m_nid);
					$this->m_txt_ccode	 				= $obj_data['ccode'];
					$this->m_txt_cproducts 				= $obj_data['cproducts'];
					$this->m_txt_cnote	 				= $obj_data['cnote'];
					$this->m_chk_nspecial_product		= $obj_data['nspecial_products'];
					$this->m_txt_cindex					= $obj_data['cindex'];
					$this->m_txt_nstatus				= $obj_data['nstatus'];
					$this->m_txt_ctag					= $obj_data['ctag'];
					//$this->m_txt_cmodel					= $obj_data['cmodel'];
					$this->m_txt_cpower					= $obj_data['cpower'];	
					$this->m_txt_cimage			 		= $obj_data['cimage'];
					$this->m_txt_cimage_resize	 		= $obj_data['cimage_resize'];
					$this->m_txt_fprice			 		= $obj_data['fprice'];
					$this->m_txt_fprice_sale	 		= $obj_data['fprice_sale'];
					//$this->m_txt_nquantity		 		= $obj_data['nquantity'];
					$this->m_txt_cdescription	 		= $obj_data['cdescription'];
					$this->m_txt_cdetail		 		= $obj_data['cdetail'];
					$this->m_cbo_nid_material_products	= $obj_data['nid_material_products'];
					$this->m_cbo_nid_cat_products		= $obj_data['nid_cat_products'];
					$this->nid_brand_products			= $obj_data['nid_brand_products'];
					
					$this->m_cbo_nid_weight				= $obj_data['nid_weight'];
				//	exit($this->m_cbo_nid_cat_products.$this->m_cbo_nid_material_products);
				$this->m_link_page 	= base_url() . 'index.php/do_product/f_update_edit';
				$this->m_form_title = $this->m_language_key('FormEditTitle').'<label style="color:#FF6633"> '.$this->m_txt_cproducts.'</label>';	
				$this->m_event 	= 'update_edit';
				break;
			case 'add':
				$this->m_form_title = $this->m_language_key('FormAddTitle');
				$this->m_event 	= 'update_add';
				break;
			case 'update_edit':
				if ($this->m_button_click == 'btn_submit')
				if ($this->update_data()==TRUE)
							redirect ('do_product_listview');
				$this->m_form_title = $this->m_language_key('FormEditTitle').'<label style="color:#FF6633"> '.$this->m_txt_cproducts.'</label>';
				
				
				$this->m_link_page 	= base_url() . 'index.php/do_product/f_update_edit';
				break;
			
			case 'update_add':
				if($this->m_txt_cimage == '' && $this->m_txt_cimage_resize == '')
				{
					$this->m_txt_cimage 				= $this->m_hidden_image_old;
					$this->m_txt_cimage_resize			= $this->m_hidden_image_old_resize;
				}
				if ($this->m_button_click == 'btn_submit')
					if ($this->insert_data()==TRUE)
						redirect ('do_product_listview');
				$this->m_link_page 	= base_url() . 'index.php/do_product/f_update_add';
				$this->m_form_title = $this->m_language_key('FormAddTitle').' '.$this->m_txt_cproducts;
					break;
		}
		
		//nhan object de chuan bi cho combobox 
		$this->m_obj_material_product_view 	= Obj_get_material_product_list($this->m_nid_user_login);
		$this->m_obj_cat_product_view 		= Obj_get_cat_product_list($this->m_nid_user_login, $this->m_cbo_nid_material_products);
		$this->m_obj_brand					= Obj_get_brand_product_list($this->m_cbo_nid_cat_products);

		//$this->m_obj_weight_view 			= Obj_get_weight_list($this->m_nid_user_login);		
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Xu ly nghiep vu
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function do_business()
	{
//He thong					
		$data['event'] 				= $this->m_event;
		$data['menu'] 				= Fget_menu_html($this->m_nid_user_login);
		$data['lbl_form_title'] 	= $this->m_form_title;
		$data['link_page'] 			= $this->m_link_page;
		$data['link_cancel'] 		= $this->m_link_cancel;		
		$data['link_cancel_trans'] 	= $this->m_link_cancel_trans;
// Ten cac button		
		$data['btn_update'] 		= $this->lang->line('btn.0000.Update');
		$data['btn_cancel'] 		= $this->lang->line('btn.0000.Cancel');
		$data['fr_img']				= Fstr_replace('admin/','',base_url());
		$data['lbl_tag'] 			= $this->lang->line('lbl.0000.Tag');
// Ky hieu dung de xac dinh cac truong thong tin khong duoc phep thieu.
		$data['get_icon_notnull']   	= Fget_icon_notnull();		
		$data['get_message_notnull']   	= Fget_icon_notnull() . $this->lang->line('msg.0000.NotNullValue');;
		
//lbl_form
		$data['lbl_form_title'] 		= $this->m_form_title;
		$data['lbl_ccode'] 	        	= $this->m_language_key('ccode');
		$data['lbl_cproducts'] 			= $this->m_language_key('cproducts');
		$data['lbl_cnote']	 			= $this->m_language_key('cnote');
		$data['lbl_cindex'] 			= $this->m_language_key('cindex');
		$data['lbl_nstatus'] 			= $this->m_language_key('nstatus');
		$data['lbl_nspecial_product']	= $this->m_language_key('nspecial_product');
		
		$data['lbl_nid_metarial_product'] = $this->m_language_key('nid_metarial_product');
		$data['lbl_nid_brand_product'] = $this->m_language_key('nid_brand_product');
		$data['lbl_nid_cat_product'] 	= $this->m_language_key('nid_cat_product');
		$data['lbl_choose_metarial_product'] 	= $this->m_language_key('choose_metarial_product');
		$data['lbl_choose_cat_product'] 		= $this->m_language_key('choose_cat_product');
		
		
		$data['lbl_cimage'] 			= $this->m_language_key('cimage');
		$data['lbl_fprice'] 			= $this->m_language_key('fprice');
		$data['lbl_fprice_sale'] 		= $this->m_language_key('fprice_sale');
		//$data['lbl_nquantity'] 			= $this->m_language_key('nquantity');
		$data['lbl_cdescription'] 		= $this->m_language_key('cdescription');
		$data['lbl_cdetail'] 			= $this->m_language_key('cdetail');
		$data['lbl_cmodel'] 			= $this->m_language_key('cmodel');
		$data['lbl_cpower'] 			= $this->m_language_key('cpower');
// lbl err
		$data['lbl_error_msg']			= $this->m_error_msg;

//Bien doi tuong 
		$data['txt_ccode']				= $this->m_txt_ccode;
		$data['txt_cproducts']			= $this->m_txt_cproducts;
		$data['txt_cnote']				= $this->m_txt_cnote;
		$data['txt_cindex']				= $this->m_txt_cindex;
		$data['txt_nstatus']			= $this->m_txt_nstatus;
		$data['chk_nspecial_product']	= $this->m_chk_nspecial_product;
		$data['txt_ctag'] 				= $this->m_txt_ctag;
		if($this->m_txt_cimage == '')
			$this->m_txt_cimage			= $this->m_hidden_image_old;
		$data['txt_cimage'] 			= $this->m_txt_cimage;
		if($this->m_txt_cimage_resize == '')
			$this->m_txt_cimage_resize	= $this->m_hidden_image_old_resize;
		$data['txt_cimage_resize'] 		= $this->m_txt_cimage_resize;
		$data['txt_fprice']				= $this->m_txt_fprice;
		$data['txt_fprice_sale']		= $this->m_txt_fprice_sale;
		//$data['txt_nquantity']			= $this->m_txt_nquantity;
		$data['txt_cdescription']		= $this->m_txt_cdescription;
		$data['txt_cdetail']			= $this->m_txt_cdetail;
		//$data['txt_cmodel']				= $this->m_txt_cmodel;
		$data['txt_cpower']				= $this->m_txt_cpower;
//Truyen bien nid cho view
		$data['nid']					= $this->m_nid;
		$data['nid_brand_products']		= $this->nid_brand_products;
		
		$data['cbo_nid_material_products']	= $this->m_cbo_nid_material_products;
		$data['material_product_view']	= $this->m_obj_material_product_view;
		
		$data['cbo_nid_cat_products']	= $this->m_cbo_nid_cat_products;
		
		$data['gen_cbo_status']			= Fget_combobox_yes_no('no','txt_nstatus',$this->m_txt_nstatus,'width:200px',$this->lang->line('lbl.0000.Yes'),$this->lang->line('lbl.0000.No'));
		
		//$data['gencbo_material_product_list']	= Fgen_html_combobox('', 'cbo_nid_material_products', $this->m_cbo_nid_material_products, '', $this->m_obj_material_product_view, 'nid', 'cmaterial_products','submit','');
		$data['gencbo_cat_product_list']		= Fgen_html_combobox('no', 'cbo_nid_cat_products', $this->m_cbo_nid_cat_products, 'width:200px', $this->m_obj_cat_product_view, 'nid', 'ccat_products','nosubmit','');
		//$data['gencbo_weight_list']		= Fgen_html_combobox('no', 'cbo_nid_weight', $this->m_cbo_nid_weight, '', $this->m_obj_weight_view, 'nid', 'cname','nosubmit','');
		$data['cbo_nid_weight']			= $this->m_cbo_nid_weight;
		$data['obj_weight_view']		= $this->m_obj_weight_view;
		$data['gencbo_brand_product_list']		= Fgen_html_combobox('no', 'nid_brand_products', $this->nid_brand_products, 'width:200px', $this->m_obj_brand, 'nid', 'cbrand_products','nosubmit','');
	
		$data['menu_active']		= 'product';
// Load view voi su kien tuong ung.
		$this->load->view('product_view/index.php',$data);
		
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Huy du lieu
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function destroy_data()
    {
        	
    }


/**
 |====================================================================
 | DANH SACH CAC HAM DINH NGHIA THEM
 |====================================================================
 */
private function check_valid_not_null()
	{
		
		// Kiem tra truong ma QL khong rong
		if($this->m_txt_ccode == '')
		{
			$this->m_error_msg	= $this->m_language_key('ccode') . $this->lang->line('msg.0000.ErorNotNull');
			return FALSE;
		}
		
		// Kiem tra truong ten san pham khong rong
		if($this->m_txt_cproducts == '')
		{
			$this->m_error_msg	= $this->m_language_key('cproducts') . $this->lang->line('msg.0000.ErorNotNull');
			return FALSE;
		}
		
		if($this->m_cbo_nid_material_products == '')
		{
			$this->m_error_msg	= $this->m_language_key('nid_metarial_product') . $this->lang->line('msg.0000.ErorNotNull');
			return FALSE;
		}
		
		if($this->m_cbo_nid_cat_products == '' OR $this->m_cbo_nid_cat_products == 0)
		{
			$this->m_error_msg	= $this->m_language_key('nid_cat_product') . $this->lang->line('msg.0000.ErorNotNull');
			return FALSE;
		}
		
		
		return TRUE;
	}
/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Kiem tra du lieu truoc khi insert
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function check_valid_before_insert()
	{
	
		if ($this->check_valid_not_null()== FALSE)
		{
			return FALSE;
		}
		if (fbcheck_exists_key_addnew('tproducts', 'ccode',$this->m_txt_ccode)==FALSE)
		{
			$this->m_error_msg	 	= $this->m_language_key('ccode') . $this->lang->line('msg.0000.ErorDoubleKey');
			return FALSE;		
		}
		if (fbcheck_exists_key_addnew('tproducts', 'cproducts',$this->m_txt_cproducts)==FALSE)
		{
			$this->m_error_msg	 	= $this->m_language_key('cproducts') . $this->lang->line('msg.0000.ErorDoubleKey');
			return FALSE;		
		}
		return TRUE;		
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Insert du lieu vao tproduct
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function insert_data()
	{
	
		if ($this->check_valid_before_insert()== TRUE)
		{
			$data =	array(								
							'ccode'						=> $this->m_txt_ccode,
							'cproducts'					=> $this->m_txt_cproducts,
							'cnote'						=> $this->m_txt_cnote,
							'nspecial_products'			=> $this->m_chk_nspecial_product,
							'nid_brand_products'		=> $this->nid_brand_products,
							'cindex'					=> $this->m_txt_cindex,
							'nstatus'					=> $this->m_txt_nstatus,
							'ncheck'					=> 0,
							'ctype'						=> 0,
							'ctag'						=> $this->m_txt_ctag,
							//'cmodel'					=> $this->m_txt_cmodel,
							'cpower'					=> $this->m_txt_cpower,
							'cimage'					=> $this->m_txt_cimage,
							'cimage_resize'				=> $this->m_txt_cimage_resize,
							'fprice'					=> $this->m_txt_fprice,
							'fprice_sale'				=> $this->m_txt_fprice_sale,
							//'nquantity'					=> $this->m_txt_nquantity,
							'cdescription'				=> $this->m_txt_cdescription,
							'cdetail'					=> $this->m_txt_cdetail,
							'nid_material_products'		=> $this->m_cbo_nid_material_products,
							'nid_cat_products'			=> $this->m_cbo_nid_cat_products,
							'nid_weight'				=> $this->m_cbo_nid_weight,		
									
							'niduser01'     			=> $this->m_nid_user_login,
							'niduser02'     			=> $this->m_nid_user_login,
							'ddate01'					=> dbget_current_date(),
							'ddate02'					=> dbget_current_date()
							);
			
			// Goi phuong thuc cap nhat thong tin vao database.	
			$this->product_model->insert($data);
			$nid_cat = dbget_identity();
			$clink= cleanText($this->m_txt_cproducts);
		$clink= khongdau($this->m_txt_cproducts);
		$nid_product = dbget_identity();
		$data2= array(	'nid_detail'=>$nid_product,
					'clink'=>$clink,
					'ctable'=>'tproduct'
		);
		$this->db->insert('tindex',$data2);
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}


/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Kiem tra du lieu truoc khi update
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function check_valid_before_update()
	{
		if ($this->check_valid_not_null()== FALSE)
			return FALSE;

		if (fbcheck_exists_key_update('tproducts', 'ccode',$this->m_txt_ccode,$this->m_nid)==FALSE)
		{
			$this->m_error_msg	 	= $this->m_language_key('ccode') . $this->lang->line('msg.0000.ErorDoubleKey');
			return FALSE;		
		}
		
		return TRUE;
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2009/12/13
 * @description		: Update du lieu vao tproduct
 * @access	        : private
 *
 * @param string	: None
 * 					: 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */		
private function update_data()
	{
		if ($this->check_valid_before_update()==TRUE)
		{
						$data =	array(
						'ccode'						=> $this->m_txt_ccode,								
						'cproducts'					=> $this->m_txt_cproducts,
						'cnote'						=> $this->m_txt_cnote,
						'nspecial_products'			=> $this->m_chk_nspecial_product,
						'nid_brand_products'		=> $this->nid_brand_products,
						'cindex'					=> $this->m_txt_cindex,
						'nstatus'					=> $this->m_txt_nstatus,
						'ctag'						=> $this->m_txt_ctag,
						//'cmodel'					=> $this->m_txt_cmodel,
						'cpower'					=> $this->m_txt_cpower,
						'fprice'					=> $this->m_txt_fprice,
						'fprice_sale'				=> $this->m_txt_fprice_sale,
						//'nquantity'					=> $this->m_txt_nquantity,
						'cdescription'				=> $this->m_txt_cdescription,
						'cdetail'					=> $this->m_txt_cdetail,
						'nid_material_products'		=> $this->m_cbo_nid_material_products,
						'nid_cat_products'			=> $this->m_cbo_nid_cat_products,
						'nid_weight'				=> $this->m_cbo_nid_weight,		
						'niduser02'     			=> $this->m_nid_user_login,
						'ddate01'					=> dbget_current_date(),
						'ddate02'					=> dbget_current_date()
	    			    );
			if($this->m_txt_cimage !='')
				{
					$path				='.././upload/images_product/full_images/';
					delfile($path.$this->m_hidden_image_old);
					$data['cimage']		=  $this->m_txt_cimage ;
				}
			if($this->m_txt_cimage_resize !='')
				{
					$path				='.././upload/images_product/resize_images/';
					delfile($path.$this->m_hidden_image_old_resize);
					$data['cimage_resize']		=  $this->m_txt_cimage_resize ;
				}
			$this->product_model->update_bynid($this->m_nid, $data);	
				
			$clink= cleanText($this->m_txt_cproducts);
			$clink= khongdau($this->m_txt_cproducts);
			$data2= array('clink'=>$clink);
			$this->db->where('nid_detail',$this->m_nid);
			$this->db->where('ctable','tproduct');
			$this->db->update('tindex',$data2);
			return TRUE; 
		}
		else
		{
			return FALSE;
		}
	}
//Load combobox cat product tu cbo metarial product khi them sua product
function load_cbo_cat_product($nid_product,$nid_metarial_product = '')
	{
		$this->load->language('ap', 'eng');
		$m_nid_product  	= $nid_product;
		$m_nid_cat_product	= '';
		if($nid_product != '' && $nid_product != 0)
		{
			$obj_data = $this->product_model->get_byid($nid_product);
				$m_nid_cat_product 	= $obj_data['nid_cat_products'];
		}
		else
			$m_nid_cat_product 	= '';
		
		$this->m_obj_cat_product_view 	= Obj_get_cat_product_list('', $nid_metarial_product);
		$data['nid_cat_products']		= $m_nid_cat_product;
		$data['nid_metarial_products']	= $nid_metarial_product;
		$data['datas'] 					= $this->m_obj_cat_product_view;
		
		$this->load->view('product_view/ap_cbo_cat_product',$data);
	}

/**
 *-------------------------------------------------------------------
 * @creator 		: Cao An Phu - phu_ca86@tocaban.com

 * @finished date	: 2010/04/10
 * @description		: ham upload hinh anh
 * @access	        : public
 *
 * @param string	: None
 *                  : 
 * @return string	: None
 *-------------------------------------------------------------------
 * @editor   	    : 
 * @finished date	: 
 * @editing content	: 
 *-------------------------------------------------------------------
 */	
	
function upload_img($file,$path)
{
	$file_name='';
	if ((($file["type"] == "image/gif") || ($file["type"] == "image/jpeg")|| ($file["type"] == "image/pjpeg"))&& ($file["size"] < 2000000))
		{
		if ($file["error"] > 0)
			{
				$msg= "Co loi khi upload anh: " . $_FILES["image2"]["error"] . "<br />";
				
 	 		}
			else
			{
			if (file_exists("upload/" . $file["name"]))
			  {
				$msg= $file["name"] . " Files da ton tai. ";
			  }
			else
			  {
			  list($name,$ext) = split('[.]',$file["name"]);
			  //Upload image vao thu muc nao
			  $file_name=md5(rand(2,999).date('Ymdhis')).'.'.$ext;
			  $this->m_txt_cimage =$file_name;
			  move_uploaded_file($file["tmp_name"],$path.$file_name);
			  }
			}
		
		}	
	return 	$file_name;
}

function ajax_get_product_cat()
{
	$nid_cat 	= '0';
	$obj_brand 	= '';
	if(isset($_POST['nid_cat']))
		$nid_cat = $_POST['nid_cat'];
	$this->db->select('nid,ccat_products');	
	$this->db->where('nid_material_products',$nid_cat);
	$obj_brand = $this->db->get('tcat_products');
	$obj_brand = $obj_brand->result_array();
	
	$str= '<select name="cbo_nid_cat_products" id="cbo_nid_cat_products" style="width:200px;" onchange="load_brand_select(this.value);">';
	$str.='<option value="" ></option>';
	foreach($obj_brand as $brand)
	{
		$str.='<option value="'.$brand['nid'].'">'.$brand['ccat_products'].'</option>';
	}
	$str.='</select>';
	
	echo $str;
}

function ajax_get_product_cat_select()
{
	$nid_cat 			= '0';
	$nid_cat_select 	= '0';
	$obj_brand 	= '';
	if(isset($_POST['nid_cat']))
		$nid_cat = $_POST['nid_cat'];
	if(isset($_POST['nid_cat_select']))
		$nid_cat_select = $_POST['nid_cat_select'];
	$this->db->select('nid,ccat_products');	
	$this->db->where('nid_material_products',$nid_cat);
	$obj_brand = $this->db->get('tcat_products');
	$obj_brand = $obj_brand->result_array();
	
	$str= '<select name="cbo_nid_cat_products" id="cbo_nid_cat_products" style="width:200px;" onchange="load_brand_select(this.value);">';
	$str.='<option value="" ></option>';
	foreach($obj_brand as $brand)
	{
		if($brand['nid']==$nid_cat_select)
			$str.='<option value="'.$brand['nid'].'" selected="selected">'.$brand['ccat_products'].'</option>';
		else
			$str.='<option value="'.$brand['nid'].'">'.$brand['ccat_products'].'</option>';
	}
	$str.='</select>';	
	echo $str;
}

function ajax_get_product_brand()
{
	$nid_cat 	= '0';
	$obj_brand 	= '';
	if(isset($_POST['nid_cat']))
		$nid_cat = $_POST['nid_cat'];
	$this->db->select('nid,cbrand_products');	
	$this->db->where('nid_cat_products',$nid_cat);
	$obj_brand = $this->db->get('tbrand_products');
	$obj_brand = $obj_brand->result_array();
	
	$str= '<select name="nid_brand_products" id="nid_brand_products" style="width:200px;">';
	$str.='<option value="" ></option>';
	foreach($obj_brand as $brand)
	{
		$str.='<option value="'.$brand['nid'].'">'.$brand['cbrand_products'].'</option>';
	}
	$str.='</select>';
	
	echo $str;
}

function ajax_get_product_brand_select()
{
	$nid_cat 			= '0';
	$nid_cat_select 	= '0';
	$obj_brand 	= '';
	if(isset($_POST['nid_cat']))
		$nid_cat = $_POST['nid_cat'];
	if(isset($_POST['nid_cat_select']))
		$nid_cat_select = $_POST['nid_cat_select'];
	$this->db->select('nid,cbrand_products');	
	$this->db->where('nid_cat_products',$nid_cat);
	$obj_brand = $this->db->get('tbrand_products');
	$obj_brand = $obj_brand->result_array();
	
	$str= '<select name="nid_brand_products" id="nid_brand_products" style="width:200px;">';
	$str.='<option value="" ></option>';
	foreach($obj_brand as $brand)
	{
		if($brand['nid']==$nid_cat_select)
			$str.='<option value="'.$brand['nid'].'" selected="selected">'.$brand['cbrand_products'].'</option>';
		else
			$str.='<option value="'.$brand['nid'].'">'.$brand['cbrand_products'].'</option>';
	}
	$str.='</select>';
	
	echo $str;
}


}


// End do_product class
	
// End of file do_product.php
// Location: controllers/do_product.php




